// pages/balance/balance.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activeIndex: 0, //默认选中第一个
    numArray: [50, 100, 200, 500, 1000,'m'],
    userBalance: [],
    couponList: [],
    rechargeMoney: 50
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this
    wx.request({
      url: 'http://localhost:8081/api/balance/getBalanceByUserId',
      method: 'GET',
      data: {
        userId : wx.getStorageSync('userInfo').id
      },
      header: {
        'token' : wx.getStorageSync('token')
      },
      success:function(res) {
        that.setData({
          userBalance: res.data.result
        })
      }
    })
    // wx.request({
    //   url: 'http://localhost:8081/api/coupon/wxGetMyCouponList',
    //   method: 'GET',
    //   data:{
    //     userId: wx.getStorageSync('userInfo').id
    //   },
    //   header:{
    //     'token': wx.getStorageSync('token')
    //   },
    //   success:function(res){
    //     console.log(res)
    //     if (res.data.code == 10008) {
    //       that.setData({
    //         couponList: res.data.result
    //       })
    //     }
    //   }
    // })
  },

  // 选择事件
  activethis: function (event) { //点击选中事件
    var that = this
    var thisindex = event.currentTarget.dataset.thisindex; //当前index
    that.setData({
      activeIndex: thisindex,
      rechargeMoney : that.data.numArray[thisindex]
    })
  },

  getRechargeMoney(e){
    var that = this
    that.setData({
      rechargeMoney : e.detail.value
    })
  },

  recharge() {
    var that = this
    console.log(that.data.rechargeMoney)
    if (that.data.rechargeMoney != '') {
      wx.request({
        url: 'http://localhost:8081/api/balance/wxRecharge',
        method: 'POST',
        data: {
          username: wx.getStorageSync('userInfo').username,
          userId: wx.getStorageSync('userInfo').id,
          rechargeMoney: that.data.rechargeMoney
        },
        header: {
          'token' : wx.getStorageSync('token')
        },
        success:function(res){
          wx.showToast({
            title: res.data.result,
            icon: 'none'
          }),
          that.onShow()
        }
      })
    }else{
      wx.showToast({
        title: '请选择金额',
        icon: 'error'
      })
    }
  },

  // 去使用
  toIndex() {
    wx.switchTab({
      url: '../index/index',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    var that = this
    wx.request({
      url: 'http://localhost:8081/api/balance/getBalanceByUserId',
      method: 'GET',
      data: {
        userId : wx.getStorageSync('userInfo').id
      },
      header: {
        'token' : wx.getStorageSync('token')
      },
      success:function(res) {
        that.setData({
          userBalance: res.data.result
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})